#####
# Replication for: "Can Political Speech Foster Tolerance of Immigrants?" by Schleiter, Tavits, and Ward.
# Table S.11
#####

#####-----#####-----#####-----
# NOTE:
# Changes to either R or lfe (or lfe's dependencies) since the original fitting in July 2019
# mean that the original code no longer exactly replicates the clustered standard errors
# as reported in the SI (for model 4, table S.11). The differences are small and do not 
# affect any substantive interpretations.
# It is hard to say what exactly is causing this, although it may be the dof used. It is important
# to note that the coefficient estimates and number of observations still exactly match.
#####-----#####-----#####-----

library(here)
library(data.table)
library(lfe)
library(texreg)

# load the behavioral data if not already in workspace
if(!exists("behav")){
  behav <- fread(here("data", "behav.csv"))
}


# Fitting -----------------------------------------------------------------

foreign_nc <- felm(
  tokens ~ CH + CS + NM + female_partner | game_NR | 0 | ResponseId,
  data = behav[foreign_partner == 1])
summary(foreign_nc)

foreign_all <- felm(
  tokens ~ CH + CS + NM + female_partner + female + age + ageSq + race2 + edu2 + pid2 + news2 + region2 + factor(wave) | game_NR | 0 | ResponseId,
  data = behav[foreign_partner == 1])
summary(foreign_all)  

foreign_imb <- felm(
  tokens ~ CH + CS + NM + female_partner + age + ageSq | game_NR | 0 | ResponseId,
  data = behav[foreign_partner == 1])
summary(foreign_imb)  

did <- felm(
  tokens ~ female_partner + foreign_partner + CH:foreign_partner + CS:foreign_partner + NM:foreign_partner |  ResponseId + game_NR | 0 | ResponseId,
  data = behav)
summary(did)

# Reporting ---------------------------------------------------------------

texreg(
  list(
    foreign_nc,
    foreign_all,
    foreign_imb,
    did
  ),
  custom.coef.map = list(
    "NM" = "Norms",
    "CS" = "Countering Stereotypes",
    "CH" = "Common Humanity",
    "female_partner" = "Female Partner",
    "foreign_partner" = "Foreign-Born Partner",
    "foreign_partner:NM" = "Foreign-Born Partner $\\times$ Norms",
    "foreign_partner:CS" = "Foreign-Born Partner $\\times$ C.S.",
    "foreign_partner:CH" = "Foreign-Born Partner $\\times$ C.H."
  ),
  booktabs = T,
  dcolumn = T,
  stars = c(0.01, .05, .1),
  caption.above = T,
  digits = 3,
  custom.model.names = paste0("(", 1:4, ")"),
  label = "tab:tokenReg",
  caption = "Regression Estimates of Treatment Effects for Behavioral Outcome",
  include.rsquared = F,
  include.adjrs = F
)
